package com.clm.controller;

import com.clm.common.ResultJson;
import com.clm.po.Restaurant;
import com.clm.vo.RestaurantVO;
import com.demo.blog.BlogInterceptor;
import com.google.gson.Gson;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

import java.util.*;

@Before(BlogInterceptor.class)
public class RestaurantController extends Controller {

    public void find(){
        String id = getPara("id");
        if(id == null || "".equals(id.trim())){
            ResultJson json = new ResultJson();
            json.setCode(1);
            json.setMessage("id不能为空!");
            Gson gson = new Gson();
            renderJson(gson.toJson(json));
        }

        Restaurant rest = Restaurant.dao.findById(id);

        ResultJson json = new ResultJson();
        if(rest != null){
            json.setCode(0);
            json.setMessage("success");

            Map<String,Object> resultMap = new HashMap<String, Object>();
            for (Map.Entry<String, Object> entry : rest.getAttrsEntrySet()) {
                resultMap.put(entry.getKey(),entry.getValue());
            }
            json.setData(resultMap);
        }else{
            json.setCode(-1);
            json.setMessage("faile");
        }
        Gson gson = new Gson();
        renderJson(gson.toJson(json));
    }

    /**
     * 根据条件分页查询餐馆
     */
	public void list() {
        int pno = getParaToInt("pno",1);
        int psize = getParaToInt("psize", 10);
        //TODO 根据餐馆名或者用户所在经纬度查询
        Page<Restaurant> list = Restaurant.dao.paginate(pno, psize);
        List<RestaurantVO> resultList = new ArrayList<RestaurantVO>(list.getList().size());
        for (Restaurant restaurant : list.getList()) {
            RestaurantVO vo = new RestaurantVO();
            vo.setName(restaurant.getStr("name"));
            vo.setAddress(restaurant.getStr("address"));
            resultList.add(vo);
        }
        ResultJson json = new ResultJson();
        json.setCode(0);
        json.setMessage("success");
        json.setData(resultList);
        Gson gson = new Gson();
        renderJson(gson.toJson(json));
	}

    public void add(){
        String name = getPara("name");
        String address = getPara("address");
        String beginTime = getPara("beginTime");
        String endTime = getPara("endTime");
        String notice = getPara("notice");
        String minPrice = getPara("minPrice");
        String introduction = getPara("introduction");

        Restaurant rest = new Restaurant();
        rest.set("name",name);
        rest.set("address",address);
        rest.set("beginTime",beginTime);
        rest.set("endTime",endTime);
        rest.set("notice",notice);
        rest.set("minPrice",Double.parseDouble(minPrice));
        rest.set("introduction",introduction);

        if(rest.save()){
            ResultJson json = new ResultJson();
            json.setCode(0);
            json.setMessage("success");
            Gson gson = new Gson();
            renderJson(gson.toJson(json));
        }
    }

    public void update(){
        String id = getPara("id");
        if(id == null || "".equals(id.trim())){
            ResultJson json = new ResultJson();
            json.setCode(1);
            json.setMessage("id不能为空!");
            Gson gson = new Gson();
            renderJson(gson.toJson(json));
        }
        String name = getPara("name");
        String address = getPara("address");
        String beginTime = getPara("beginTime");
        String endTime = getPara("endTime");
        String notice = getPara("notice");
        String minPrice = getPara("minPrice");
        String introduction = getPara("introduction");

        Restaurant rest = new Restaurant();
        rest.set("name",name);
        rest.set("address",address);
        rest.set("beginTime",beginTime);
        rest.set("endTime",endTime);
        rest.set("notice",notice);
        rest.set("minPrice",Double.parseDouble(minPrice));
        rest.set("introduction",introduction);

        if(rest.update()){
            ResultJson json = new ResultJson();
            json.setCode(0);
            json.setMessage("success");
            Gson gson = new Gson();
            renderJson(gson.toJson(json));
        }
    }

    /**
     * 根据餐馆id删除餐馆
     */
    public void delete(){
        String id = getPara("id");
        if(id == null || "".equals(id.trim())){
            ResultJson json = new ResultJson();
            json.setCode(1);
            json.setMessage("id不能为空!");
            Gson gson = new Gson();
            renderJson(gson.toJson(json));
        }

        boolean isDelete = Restaurant.dao.deleteById(id);

        ResultJson json = new ResultJson();
        if(isDelete){
            json.setCode(0);
            json.setMessage("success");
        }else{
            json.setCode(-1);
            json.setMessage("faile");
        }
        Gson gson = new Gson();
        renderJson(gson.toJson(json));
    }

}


